package plgdb

import (
	"dgo/MyProj/PrjDBUtils"
	"gitee.com/ymofen/supervalue"
)

type PlgDBQryParam struct {
	HttpReq         *PlgDBRequest
	ResID           string
	DataSourceid    string
	DBName          string
	SingleName      string
	TraceSQL        int
	Req             *wrapper.SuperValue
	Resp            *wrapper.SuperValue
	DBResConf       *wrapper.SuperValue
	Args            *wrapper.SuperValue
	Dao             *PrjDBUtils.DAOHelper
	GetParamValFunc func(sender interface{}, key string) (bool, interface{})
}

func NewDBQryParma() *PlgDBQryParam {
	rval := &PlgDBQryParam{}
	return rval
}

func (this *PlgDBQryParam) Close() error {
	if this.Dao != nil {
		this.Dao.Close()
		this.Dao = nil
	}
	return nil
}

func (this *PlgDBQryParam) GetParamVal(key string) (ok bool, val interface{}) {
	ok = false
	if this.HttpReq.OnGetParamValFunc != nil {
		ok, val = this.HttpReq.OnGetParamValFunc(this, key)
	}
	if !ok && this.GetParamValFunc != nil {
		ok, val = this.GetParamValFunc(this, key)
	}

	if !ok && this.Args != nil {
		v := this.Args.ValueByName(key)
		if v != nil {
			return true, v.Variant()
		}
	}

	return
}

func (this *PlgDBQryParam) ReqStringByName(key string, def string) string {
	if this.Req == nil {
		return def
	}
	return this.Req.StringByName(key, def)
}

func (this *PlgDBQryParam) ReqIntByName(key string, def int) int {
	if this.Req == nil {
		return def
	}
	return this.Req.IntByName(key, def)
}
